package main

import (
	"device/sdk"
	"github.com/judwhite/go-svc/svc"
	"log"
	"os"
)

// implements svc.Service
type program struct {
	LogFile *os.File
	svr     *sdk.Device
}

func main() {
	prg := program{
		svr:&sdk.Device{},
	}
	defer func() {
		if prg.LogFile != nil {
			if closeErr := prg.LogFile.Close(); closeErr != nil {
				log.Printf("error closing '%s': %v\n", prg.LogFile.Name(), closeErr)
			}
		}
	}()

	// call svc.Run to start your program/service
	// svc.Run will call Init, Start, and Stop
	if err := svc.Run(&prg); err != nil {
		log.Fatal(err)
	}
}

func (p *program) Init(env svc.Environment) error {
	log.Printf("iot device init ...")

	p.svr.Init("")

	if p.svr.Err != nil {
		log.Fatal(" device init err : ",p.svr.Err)
	}
	return nil
}

func (p *program) Start() error {
	log.Printf("Starting...\n")
	p.svr.Start()
	return nil
}

func (p *program) Stop() error {
	log.Printf("Stopping...\n")
	 p.svr.Stop();
	log.Printf("Stopped.\n")
	return nil
}

